package com.liuxu.topnews.floating;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import com.liuxu.topnews.MyApplication;

import static com.liuxu.topnews.MyApplication.heightPixels;
import static com.liuxu.topnews.MyApplication.widthPixels;

public class FloatingManager {
    private static FloatingManager Instance;
    private WindowManager windowManager;
    private static FloatingView floatingView;
    private static LayoutParams floatingViewParams;

    private FloatingManager() {
        this.windowManager = (WindowManager)MyApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
    }

    public static FloatingManager getInstance() {
        if (Instance == null) {
            Instance = new FloatingManager();
        }

        return Instance;
    }

    protected boolean addView(View view, LayoutParams layoutparams) {
        try {
            this.windowManager.addView(view, layoutparams);
            return true;
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }

    protected boolean removeView(View view) {
        try {
            this.windowManager.removeView(view);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    protected boolean updateView(View view, LayoutParams layoutparams) {
        try {
            this.windowManager.addView(view, layoutparams);
            return true;
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }
    public void updateData( String appname,String total_time ,String time){
        if (floatingView!=null){
            floatingView.updateData(appname,total_time,time);
        }
    }

    public void show() {
        if (floatingView==null){
            floatingView=new FloatingView(MyApplication.getContext());
            if (floatingViewParams==null){
                floatingViewParams=new LayoutParams();
                floatingViewParams.type=LayoutParams.TYPE_PHONE;
                floatingViewParams.format= PixelFormat.RGBA_8888;
                floatingViewParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL|LayoutParams.FLAG_NOT_FOCUSABLE;
                floatingViewParams.gravity= Gravity.LEFT|Gravity.TOP;
                floatingViewParams.width=floatingView.viewWidth;
                floatingViewParams.height=floatingView.viewHeight;
                floatingViewParams.x=0;//widthPixels;
                floatingViewParams.y=heightPixels/2;
            }
            floatingView.setmParams(floatingViewParams);
            addView(floatingView,floatingViewParams);
        }
    }
    public void remove(){
        if (floatingView!=null){
            removeView(floatingView);
            floatingView=null;
        }
    }
}
